package com.ysz.learn.selflearn.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.ysz.learn.selflearn.annotation.Auth;

import lombok.extern.slf4j.Slf4j;

/**
 * 验证Auth切面
 *
 * @author: yushizhong
 * @date: 2021-10-28 17:26
 * @version: 1.0
 **/
@Aspect
@Component
@Slf4j
public class CheckAuthAspect {
    @Pointcut("@annotation(com.ysz.learn.selflearn.annotation.Auth)")
    void access() {
        log.info("CheckAuthAspect.access...");

    }

    @Before("@annotation(iauth)")
    void around(JoinPoint joinPoint, Auth iauth) {
        log.info("CheckAuthAspect.around....{}", iauth.desc());
        Object[] param = joinPoint.getArgs();

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < param.length; i++) {
            if (i == param.length - 1) {
                sb.append(param[i]);
            } else {
                sb.append(param[i] + "; ");
            }

        }

        System.err.println("登录用户token" + sb.toString());
        // 检验是否登录 true 已经登录 false 未登录

        Boolean flag = false;
        if (!StringUtils.isEmpty(param) && sb.toString().equals("aaa")) {
            flag = true;

        }
        if (!flag) {
            throw new RuntimeException("登录失败！");
        }

    }

    @AfterReturning(returning = "result", pointcut = "access()")
    void afterLogin(Object result) {
        System.err.println("登录结果.." + result);

    }

}
